#!/bin/bash

#
# This file is part of ExcepGen.
# Copyright (c) 2008, 2009, Iparelan Solutions, LLC. All rights reserved.
#
# ExcepGen is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# ExcepGen is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ExcepGen. If not, see <http://www.gnu.org/licenses/>.
#

#
# This top-level build script manages the automatic generation and invocation
# of the bulk of the Ant build script code. The Ant build is invoked by
# passing all arguments of this script to the generated Ant build script.
#
# The generated Ant build script code is a function of some basic information
# written in a custom build script combined with several conventions for
# component directory structure. The many of these conventions are encoded in
# an XSLT transformation script that converts the custom build script to the
# generated Ant build script.
#

GENERATED_BUILD_SCRIPT=build-generated.xml

#
# Step 1.
#
# Generate the generated build script with an XSLT stylesheet transformation.
#
# The output of this script is suppressed by default because otherwise it
# displays this misleading message almost immediately after a build begins:
#
# BUILD SUCCESSFUL
# Total time: 0 seconds
#
# Suppressing output is almost always a Bad Thing because it can lead to
# silent failures. TODO: store the output to some temp file, create a temp
# file with the above message, compare the file, display the output content if
# it is not identical to the file with the success message.
#
ant -f meta-build.xml > /dev/null

#
# Step 2.
#
# Inspect the Ant arguments to decide whether the Ant build file generated in
# step 1 should be destroyed in an "all-clean" operation.
#
ALL_CLEAN=0
for ARG in $* ; do
    [ $ARG == 'all-clean' ] && ALL_CLEAN=1
done

#
# Step 3.
#
# Execute the build proper by invoking the script created in step 1. All
# arguments of this script are passed directly to the generated Ant script.
#
ant -f build-generated.xml "$@"

#
# Step 4.
#
# Conditionally destroy the generated Ant file.
#
if [ ${ALL_CLEAN} -eq 1 ] ; then
    rm ${GENERATED_BUILD_SCRIPT}
fi
